﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading;
using RoverStudio.Common.PositionSource;

namespace WebcamDShow
{
    public partial class WebcamDShowUC : UserControl, IDisposable
    {

        private AForgeVideoSource aForgeVideoSource;
        public IVideoSourceCommon VideoSource
        {
            get { return aForgeVideoSource; }
        }

        public WebcamDShowUC()
        {
            InitializeComponent();
            aForgeVideoSource = new AForgeVideoSource("Video Input ("+this.GetHashCode().ToString()+")");
            VideoSourceCommonInstancesHolder.SingletonInstance.Register(aForgeVideoSource);
            //aForgeVideoSource.NewFrame += new NewFrameDelegate(aForgeVideoSource_NewFrame);
            videoWindow.VideoSource = aForgeVideoSource;
        }

        //void aForgeVideoSource_NewFrame(Bitmap frame)
        //{
        //    //pbPreview.Image = frame;
        //}

        private void btnSelectVideoSource_Click(object sender, EventArgs e)
        {
            VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();

            if (form.ShowDialog(this) == DialogResult.OK)
            {
                aForgeVideoSource.SetWebcamSource(form.VideoDevice);
                
            }
        }

        private void btnLocalVideo_Click(object sender, EventArgs e)
        {
            if (ofdVideoFile.ShowDialog() == DialogResult.OK)
            {
                // create video source
                aForgeVideoSource.SetFileSource(ofdVideoFile.FileName);
                txtVideoFilePath.Text = ofdVideoFile.FileName;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            // set busy cursor
            this.Cursor = Cursors.WaitCursor;
            aForgeVideoSource.DesiredWidth = Int32.Parse(txtWidth.Text);
            aForgeVideoSource.DesiredHeight = Int32.Parse(txtHeight.Text);
            aForgeVideoSource.DesiredFrameRate = Int32.Parse(txtFPS.Text);

            aForgeVideoSource.OpenVideoSource();

            this.Cursor = Cursors.Default;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            aForgeVideoSource.CloseVideoSource();
        }


        #region IDisposable Members

        void IDisposable.Dispose()
        {
            aForgeVideoSource.CloseVideoSource();
            VideoSourceCommonInstancesHolder.SingletonInstance.UnRegister(aForgeVideoSource);
            aForgeVideoSource = null;
        }

        #endregion

        private void btnSelectVideoFormat_Click(object sender, EventArgs e)
        {
            if (!(aForgeVideoSource.VideoSource is VideoCaptureDevice))
            {
                MessageBox.Show("Invalid video source!");
                return;
            }
            unsafe
            {
                (aForgeVideoSource.VideoSource as VideoCaptureDevice).DisplayPropertyPage(new IntPtr(null));
            }
        }

    }
}
